<?php
namespace c\ex;

/**
 * Description of score
 *
 * @author YeFng
 */
class score extends \control
{
    var $table = 'ex_score';
    var $auth_m = 'ex\score';
    
    var $class = array('PU'=>'\c\auth\permission');
	
	//获取推荐层次
	protected function rgn()
    {
        $g = include(CONFIG_DIR . 'ex.php');
        return $g['user_relation_g'];
    }
            
    function index_home()
    {
        $s = $this->P_G('s');   
        $t = array('t1'=>array('as'=>$this->table),
                    'f_user'=>array('as'=>'user','on'=>'t1.f_user_id = f_user.id', 'join'=>'LEFT JOIN'),
                    'user'=>array('on'=>'t1.user_id = user.id', 'join'=>'LEFT JOIN'));
        $w = '';
        if (!$this->PU->check($this->auth_m, TRUE))
		 {
			 if ($s['user_name'])
             {
				$pid = $sid = \YF_f::initC ('\c\user\g')->nameToID($s['user_name']);
                $g = $this->rgn();
				while ($pid!=\YF_f::$login_user_id)
				 {
					if($g==0 or !$pid)
                        $this->clue (\lang::g('action deny'));
					
					$pid = $this->db->getOneField('user','parent_id','id='.$pid);
					$g--;
				 }
				
				$w .= ' AND t1.user_id ='. $sid;
             }
            else
                $w .= ' AND t1.user_id ='. \YF_f::$login_user_id;
		 }
        elseif ($s['user_name']) 
            $w .= ' AND t1.user_id ='. \YF_f::initC ('\c\user\g')->nameToID($s['user_name']);
		
        if ($s['month'])
            $w .= ' AND t1.time like "'. $this->db->escape($s['month']) .'%"';
        if ($s['f_user_name'])
            $w .= ' AND t1.f_user_id ='. \YF_f::initC ('\c\user\g')->nameToID($s['f_user_name']);
        
        if ($w)
            $w = substr ($w, 4);
        
        $vlaueSum = $this->db->getOneField($t, 'sum(t1.value)', $w);
        $res = $this->db->get_page(array(), $t, 't1.*, user.name userName, f_user.name f_userName', $w, 'id DESC');
        
		$this->loadTemplate('ex/score', array('res'=>$res, 's'=>$s, 'vlaueSum'=>$vlaueSum));
		$this->display('user');
    }
    
    function add($val, $note)
    {
        $user_id = \YF_f::$login_user_id;
        $d = array('value'=>$val,
                    'note'=>$note,
                    'f_user_id'=>$user_id);
        
        $g = $g2 = $this->rgn();
        do{
            $d['user_id'] = $user_id;
            $d['note'] = $note;
			if ($g)
				$d['note'] .= ' || g' . ($g2 - $g);
			
            $b = $this->db->save($this->table, $d);
            if (!$b)
                return;
            
            //级别
            $this->db->upField('user', 'ex_grade_point', $user_id, intval($val));			
            $u = $this->db->getOne('user', '*', 'id='.$user_id);
            $ug = $this->db->getOne('user_grade', '*', 'id>'.$u['grade_id'], 'id');
            if ($u['ex_grade_point']>=$ug['point'])
                $this->db->save('user', array('grade_id'=>$ug['id']), 'id='.$user_id);
           
            //next
            $user_id = $u['parent_id'];
            $g--;
        }while ($g>=0 && $user_id);
        
        return TRUE;
    }
}
